#include <bits/stdc++.h>
using namespace std;
inline int read()
{
    int ans = 0, f = 0;
    char c = getchar();
    while (!isdigit(c))
        f |= c == '-', c = getchar();
    while (isdigit(c))
        ans = (ans << 3) + (ans << 1) + c - 48, c = getchar();
    return f ? -ans : ans;
}
void write(int x)
{
    if (x < 0)
        putchar('-'), x = -x;
    if (x > 9)
        write(x / 10);
    putchar(48 + x % 10);
}
const int N = 1e7 + 5, mod = 1e9 + 7;
int n, sum[N], mp[10];
signed main()
{
    freopen("give.in", "r", stdin);
    freopen("give.out", "w", stdout);
    auto m = [](int x)
    {
        for (int i = 0; i <= 9; ++i)
            mp[i] = 0;
        while (x)
            ++mp[x % 10], x /= 10;
        int mx = 0;
        for (int i = 0; i <= 9; ++i)
            mx = max(mx, mp[i]);
        for (int i = 9; i >= 0; --i)
            if (mp[i] == mx)
                return i;
        return 0;
    };
    for (int i = 1; i <= 10000000; ++i)
        sum[i] = sum[i - 1] + m(i);
    int T = read();
    while (T--)
        write(sum[read()]), putchar('\n');
    return 0;
}